/*
* ECARX Technology Limited is the owner of the copyright and the trade secret of this software.
* Without permission, no one has the right to obtain, disclose or use this software in any way.
*/

package ecarx.xsf.notificationcenter.utils;

import android.app.ActivityManager;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.text.TextUtils;
import android.util.Log;

import androidx.annotation.NonNull;

import java.util.List;


public class AppUtils {

    /**
     * 根据进程 ID 获取进程名
     *
     * @param pid 程 ID
     * @return 进程名称
     */
    public static @NonNull
    String getProcessName(Context context, int pid) {
        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> processInfoList = null;
        if (am != null) {
            processInfoList = am.getRunningAppProcesses();
        }
        if (processInfoList == null) {
            return "";
        }
        for (ActivityManager.RunningAppProcessInfo processInfo : processInfoList) {
            if (processInfo.pid == pid) {
                return processInfo.processName == null ? "" : processInfo.processName;
            }
        }
        return "";
    }

    public static boolean isAppPkgExist(Context context, String pkgname){
        if (TextUtils.isEmpty(pkgname)) {
            return false;
        }
        try {
            ApplicationInfo info = context.getPackageManager().getApplicationInfo(pkgname,PackageManager.GET_UNINSTALLED_PACKAGES);
            return true;
        }catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
}
